package vn.edu.saigontech.ncms.model.comparator;

import java.util.Comparator;

import vn.edu.saigontech.ncms.model.bookstore.Item;
import vn.edu.saigontech.ncms.model.bookstore.ItemTitle;
import vn.edu.saigontech.ncms.model.bookstore.ItemType;
import vn.edu.saigontech.ncms.model.program.Level;
import vn.edu.saigontech.ncms.model.program.Program;

public class ItemComparator implements Comparator<Item> {

	@Override
	public int compare(Item item1, Item item2) {
		ItemTitle title1 = item1.getItemTitle();
		ItemTitle title2 = item2.getItemTitle();
		Level level1 = title1.getLevels().iterator().next();
		Level level2 = title2.getLevels().iterator().next();
		Program program1 = level1.getProgram();
		Program program2 = level2.getProgram();
		if (program1.equals(program2)) {
			if (level1.equals(level2)) {
				if (title1.getId() == title2.getId()) {
					ItemType type1 = item1.getItemType();
					ItemType type2 = item2.getItemType();
					return type1.getOrder() - type2.getOrder();
				} else {
					return (int) (title1.getId() - title2.getId());
				}
			} else
				return level1.getOrder() - level2.getOrder();
		} else
			return program1.getOrder() - program2.getOrder();
	}
}